package com.gz.EuchreU.Controls;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.input.touch.TouchEvent;

import android.util.Log;

import com.gz.EuchreU.Contants.GameOptions;
import com.gz.EuchreU.Contants.GameState;
import com.gz.EuchreU.Contants.Suits;
import com.gz.EuchreU.Helpers.Textures;
import com.gz.EuchreU.Sprite.ButtonSprite;

public class CallSuitMenu extends MovingMenuEntity {

	ButtonSprite btnDiamonds;
	ButtonSprite btnClubs;
	ButtonSprite btnHearts;
	ButtonSprite btnSpades;
	ButtonSprite btnPass;
	
	public CallSuitMenu(float pOnScreenX, float pOnScreenY, float pWidth,
			Scene pScene) {
		super(pOnScreenX, pOnScreenY, pWidth, false);

		float buffer = 10;
		float w = (pWidth - 5 * buffer) / 4;
		float x = pOnScreenX + buffer;
		float y = pOnScreenY;

		// mOnScreenLocation = new Vector2(10, UI.getHeight()/2);

		btnSpades = new ButtonSprite(x, y, w, Textures.SPADE_TEXTURE) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP) {
					if (mActive) {
						log("Spades pressed");
						
						//TODO: Alone?
						GameState.CallSuit(Suits.SPADES, false);
					}
				}
				return true;
			}

		};

		x += w + buffer;

		btnDiamonds = new ButtonSprite(x, y, w, Textures.DIAMOND_TEXTURE) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP) {
					if (mActive) {
						log("Diamonds pressed");
						//TODO: Alone?
						GameState.CallSuit(Suits.DIAMONDS, false);
					}
				}
				return true;
			}
		};

		x += w + buffer;

		btnClubs = new ButtonSprite(x, y, w, Textures.CLUB_TEXTURE) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP) {
					if (mActive) {
						log("Clubs pressed");
						//TODO: Alone?
						GameState.CallSuit(Suits.CLUBS, false);
					}
				}
				return true;
			}
		};

		x += w + buffer;

		btnHearts = new ButtonSprite(x, y, w, Textures.HEART_TEXTURE) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP) {
					if (mActive) {
						log("Hearts pressed");
						//TODO: Alone?
						GameState.CallSuit(Suits.HEARTS, false);
					}
				}
				return true;
			}
		};

		btnPass = new ButtonSprite(pOnScreenX + pWidth/2 - w/2,y + btnDiamonds.getHeight(),w,Textures.PASS_TEXTURE) {
			
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,float pTouchAreaLocalX,float pTouchAreaLocalY){
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP){
					if(mActive){
						log("pass button pressed");
						GameState.Pass();
					}
					else{
						log("pass button pressed while inactive...");
					}
				}
				return true;
			}
		};
		
		attachChild(btnPass);
		attachChild(btnSpades);
		attachChild(btnDiamonds);
		attachChild(btnClubs);
		attachChild(btnHearts);
		
		pScene.registerTouchArea(btnPass);
		pScene.registerTouchArea(btnSpades);
		pScene.registerTouchArea(btnClubs);
		pScene.registerTouchArea(btnDiamonds);
		pScene.registerTouchArea(btnHearts);
	}

	public void Update(){
		if(GameOptions.GetBoolOption(GameOptions.PREF_BOOL_STD) && GameState.getDealer() == GameState.getCurrentPlayer()){
			log("Cant pass. STD + D=" + GameState.getDealer() + " curP=" + GameState.getCurrentPlayer());
			btnPass.SetEnabled(false);
		}
		else btnPass.SetEnabled(true);
		
		if(GameOptions.GetBoolOption(GameOptions.PREF_BOOL_CALL_TURN) == false){
			int suitID = Suits.NONE;
			if(GameState.GetTurnCard() != null){
				suitID = GameState.GetTurnCard().GetSuitID();
			}
			switch (suitID) {
			case Suits.CLUBS:
				btnClubs.SetEnabled(false);
				btnSpades.SetEnabled(true);
				btnHearts.SetEnabled(true);
				btnDiamonds.SetEnabled(true);
				break;
			case Suits.DIAMONDS:
				btnDiamonds.SetEnabled(false);
				btnSpades.SetEnabled(true);
				btnHearts.SetEnabled(true);
				btnClubs.SetEnabled(true);
				break;
			case Suits.HEARTS:
				btnHearts.SetEnabled(false);
				btnSpades.SetEnabled(true);
				btnDiamonds.SetEnabled(true);
				btnClubs.SetEnabled(true);
				break;
			case Suits.SPADES:
				btnSpades.SetEnabled(false);
				btnHearts.SetEnabled(true);
				btnDiamonds.SetEnabled(true);
				btnClubs.SetEnabled(true);
				break;	
			default:
				break;
			}
		}
		
	}
	
	private void log(String msg) {
		Log.d("CallSuitMenu", msg);

	}

}
